<?php


namespace app\backend\services;

use App\backend\models\BigWheelPrizeModel;
use App\backend\models\BigWheelModel;
use App\backend\models\GoodsModel;
use App\common\services\BaseServices;

class BigWheelPrizeServices extends BaseServices
{
    //奖品类型：商品 微券 积分 余额
    const PRIZE_GODOS = 1;
    const PRIZE_COUPON = 2;
    const PRIZE_POINT = 3;
    const PRIZE_BALANCE = 4;
    public static $Prize_Type = [
        self::PRIZE_GODOS => ['type' => 'goods', 'name' => '商品'],
        self::PRIZE_COUPON => ['type' => 'coupon', 'name' => '微券'],
        self::PRIZE_POINT => ['type' => 'point', 'name' => '积分'],
        self::PRIZE_BALANCE => ['type' => 'balance', 'name' => '余额'],
    ];

    /**
     * 处理奖品类型
     * @param $type
     * @param $request
     * @return array|mixed|null
     */
    public function setPrizeInfo($type, $request)
    {
        if (empty($type)) {
            $msg = '奖品类型不能为空';
            return self::returnError(40265, $msg);
        }
        $res = '';
        switch ($type) {
            case self::PRIZE_GODOS:
                $res = self::CheckGoodsPrizeParam($request);
                break;
            case self::PRIZE_COUPON:
                $res = self::CheckCouponPrizeParam($request);
                break;
            case self::PRIZE_POINT:
                $res = self::CheckPointPrizeParam($request);
                break;
            case self::PRIZE_BALANCE:
                $res = self::CheckBalancePrizeParam($request);
                break;
        }
        if ($res) {
            return $res;
        }
        //验证公共参数
        $result = self::CheckPrizeParam($request);
        if ($result) {
            return $result;
        }
        return self::returnSuccess('ok', $request);
    }

    /**
     * 所有奖品类型公共参数
     * @param $request
     * @return array|void
     */
    public function CheckPrizeParam($request)
    {
        if (!isset($request['mode'])) {
            $msg = '请选择抽奖模式';
            return self::returnError(40265, $msg);
        }
        if (empty($request['activity_id'])) {
            $msg = '请选择添加奖品的活动';
            return self::returnError(40261, $msg);
        }
        if (empty($request['name'])) {
            $msg = '请输入奖品名称';
            return self::returnError(40262, $msg);
        }
        if (empty($request['image'])) {
            $msg = '请上传奖品图片';
            return self::returnError(40263, $msg);
        }
        if (empty($request['inventory'])) {
            $msg = '请输入奖品库存';
            return self::returnError(40264, $msg);
        }
        if (!isset($request['win_chance'])) {
            $msg = '中奖概率不能为空';
            return self::returnError(40265, $msg);
        }
        //验证中奖概率
        $result = Self::checkPrizeProbability($request['activity_id'],$request['win_chance']);
        if ($result) {
            return $result;
        }
    }

    /**
     * 奖品商品参数处理
     * @param $request
     * @return array|null
     */
    public function CheckGoodsPrizeParam(&$request)
    {
        //商品
        if (empty($request['goods_id']) || empty($request['goods_name'])) {
            $msg = '请选择商品';
            return self::returnError(40266, $msg);
        }
        $request['prize_content_id'] = $request['goods_id'];
        $request['prize_content'] = $request['goods_name'];
        $request['prize_type'] = self::PRIZE_GODOS;
    }

    /**
     * 奖品微券参数处理
     * @param $request
     * @return array|null
     */
    public function CheckCouponPrizeParam(&$request)
    {
        //微券
        if (empty($request['coupon_id']) || empty($request['coupon_name'])) {
            $msg = '请选择微券';
            return self::returnError(40264, $msg);
        }
        $request['prize_content_id'] = $request['coupon_id'];
        $request['prize_content'] = $request['coupon_name'];
        $request['prize_type'] = self::PRIZE_COUPON;
    }

    /**
     * 奖品积分参数处理
     * @param $request
     * @return array|null
     */
    public function CheckPointPrizeParam(&$request)
    {
        if (empty($request['point'])) {
            $msg = '请填写积分数量';
            return self::returnError(40252, $msg);
        }
        $request['prize_content_id'] = 0;
        $request['prize_content'] = $request['point'];
        $request['prize_type'] = self::PRIZE_POINT;
    }

    /**
     * 奖品余额金额参数处理
     * @param $request
     * @return array|null
     */
    public function CheckBalancePrizeParam(&$request)
    {
        if (empty($request['amount'])) {
            return self::returnError(40252, '请填写余额金额');
        }
        $request['prize_content_id'] = 0;
        $request['prize_content'] = $request['amount'];
        $request['prize_type'] = self::PRIZE_BALANCE;
    }


    //整理奖品保存参数
    public function SavePrizeParam($request, &$data)
    {
        $data = [
            'activity_id' => $request['activity_id'],
            'prize_image' => $request['image'],
            'prize_name' => $request['name'],
            'prize_type' => $request['prize_type'],
            'prize_content' => $request['prize_content'],
            'prize_content_id' => $request['prize_content_id'],
            'prize_inventory' => $request['inventory'],
            'win_chance' => $request['win_chance'],
            'sort' => isset($request['sort']) ?: 0,
        ];
    }

    /**
     * 验证修改参数
     * @return void
     */
    public function checkModifyParam($request)
    {
        if (!isset($request['prize_id']) || empty($request['prize_id'])) {
            return self::returnError(40252, '参数不能为空');
        }
        $info = BigWheelPrizeModel::getInstance()->getInfo(['prize_id' => $request['prize_id']], ['activity_id', 'prize_type']);
        if (empty($info)) {
            return self::returnError(40253, '奖品不存在');
        }
        $act_info = BigWheelModel::getInstance()->getInfo(['activity_id' => $info->activity_id]);
        if (empty($act_info)) {
            return self::returnError(40254, '活动不存在');
        }

        switch ($info->prize_type) {
            case self::PRIZE_GODOS | self::PRIZE_COUPON:
                if (empty($request['prize_content_id']) || empty($request['prize_content'])) {
                    return self::returnError(40264, '奖品信息不能为空');
                }
                break;
            case self::PRIZE_POINT | self::PRIZE_BALANCE:
                if (empty($request['prize_content'])) {
                    return self::returnError(40265, '奖品信息不能为空');
                }
                break;
        }
        $request['prize_type'] = $info->prize_type;
        //验证公共参数
        $result = self::CheckPrizeParam($request);
        if ($result) {
            return $result;
        }
        return self::returnSuccess('ok', $request);
    }


    /**
     * 获取奖品列表
     * @param $activity_id
     * @return void
     */
    public function getPrizeList($activity_id)
    {
        //列表
        $lists = BigWheelPrizeModel::getInstance()->getList(['activity_id' => $activity_id]);
        // extract($lists);
        if (!empty($lists)) {
            foreach ($lists as &$val) {
                $val->prize_type_name = self::$Prize_Type[$val->prize_type]['name'];
            }
        }
        return $lists;
    }

    /**
     * 获取奖品详情
     * @param $prize_id
     * @return void
     */
    public function getPrizeInfo($prize_id)
    {
        //详情
        $info = BigWheelPrizeModel::getInstance()->getInfo(['prize_id' => $prize_id]);
        if (!empty($info) && !empty($info->prize_content_id)) {
            if ($info->prize_type = self::PRIZE_GODOS) {
                //单个商品查询 ：商品图片  商品名称  商品价格  商品库存
                //'id', 'title', 'thumb', 'thumb_url', 'type', 'price', 'stock'
                $goods_info = GoodsModel::info(['id' => $info->prize_content_id],['id', 'title', 'thumb', 'thumb_url', 'type', 'price','stock']);
                $info->content_info = $goods_info;
            } elseif ($info->prize_type = self::PRIZE_COUPON) {
                //单个优惠券数据

            }

        }
        return $info;
    }

    /**
     * 添加奖品数据
     * @param $request
     * @return bool|string
     */
    public function insertData($request)
    {
        $data = [];
        self::SavePrizeParam($request, $data);
        return BigWheelPrizeModel::getInstance()->addSave($data);
    }

    /**
     * 更新奖品信息
     * @param $prize_id
     * @return array
     */
    public function updatePrizeData($request)
    {
        $data = [];
        $prize_id = $request['prize_id'];
        self::SavePrizeParam($request, $data);
        //更新
        return BigWheelPrizeModel::getInstance()->updateSave(['prize_id' => $prize_id], $data);
    }

    /**
     * 删除奖品
     * @param $prize_id
     * @return array
     */
    public function deletePrizeData($prize_id)
    {
        if (!self::prizeExists($prize_id)) {
            return self::returnError(40226, '数据不存在');
        }
        //删除
        $res = BigWheelPrizeModel::getInstance()->deleteData(['prize_id' => $prize_id]);
        if ($res) {
            return self::returnSuccess('ok');
        } else {
            return self::returnError(40227, '删除失败');
        }
    }

    /**
     * 验证奖品数据是否存在
     * @param $id
     * @return array
     */
    public function prizeExists($id)
    {
        $info = BigWheelPrizeModel::getInstance()->getInfo(['prize_id' => $id], ['prize_id']);
        return $info;
    }

    /**
     * 验证活动奖品概率 是否超出100
     * @param int $id
     * @param float $num
     * @return array|void
     */
    public function checkPrizeProbability(int $id,float $num=0)
    {
        $sum = BigWheelPrizeModel::getInstance()->getChanceSum(['activity_id' => $id]);
        if (bcadd($sum,$num,2) > 100) {
            $msg = ' 中奖概率不能超过100% ';
            return self::returnError(40266, $msg);
        }
    }


}
